package cn.hahah.module.blog.dal.mysql.users;

import java.util.*;

import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.hahah.framework.mybatis.core.mapper.BaseMapperX;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import org.apache.ibatis.annotations.Mapper;
import cn.hahah.module.blog.controller.admin.users.vo.*;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 博客系统用户 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface UsersMapper extends BaseMapperX<UsersDO> {

    default PageResult<UsersDO> selectPage(UsersPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<UsersDO>()
                .likeIfPresent(UsersDO::getUsername, reqVO.getUsername())
                .eqIfPresent(UsersDO::getPassword, reqVO.getPassword())
                .likeIfPresent(UsersDO::getNickname, reqVO.getNickname())
                .eqIfPresent(UsersDO::getEmail, reqVO.getEmail())
                .eqIfPresent(UsersDO::getPhone, reqVO.getPhone())
                .eqIfPresent(UsersDO::getSex, reqVO.getSex())
                .eqIfPresent(UsersDO::getAvatar, reqVO.getAvatar())
                .betweenIfPresent(UsersDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(UsersDO::getId));
    }

    @Select("SELECT * FROM blog_users WHERE username = #{username}")
    List<UsersDO> selectByUsername(String username);


    @Update("UPDATE blog_users SET password = #{password}, update_time = NOW() WHERE id = #{id}")
    int updatePasswordById(Long id, String password);

}